#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int n = 0;
    int m = 0;
    int dx[8] = { -1,-1,-1,0,0,+1,+1,+1 };
    int dy[8] = { -1,0,+1,-1,+1,-1,0,+1 };
    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
        if (board[click[0]][click[1]] == 'M')
        {
            board[click[0]][click[1]] = 'X';
            return board;
        }
        n = board.size();
        m = board[0].size();

        dfs(board, click[0], click[1]);

        return board;
    }

    void dfs(vector<vector<char>>& board, int x, int y)
    {
        if (x < 0 || x == n || y < 0 || y == m || board[x][y] != 'E')
            return;

        int count = 0;
        for (int i = 0; i < 8; ++i)
        {
            count += BooM(board, x + dx[i], y + dy[i]);
        }
        if (count == 0)
        {
            board[x][y] = 'B';
            for (int i = 0; i < 8; ++i)
            {
                dfs(board, x + dx[i], y + dy[i]);
            }
        }
        else
        {
            board[x][y] = count + 48;
        }
    }

    int BooM(vector<vector<char>>& board, int x, int y)
    {
        if (x < 0 || x == n || y < 0 || y == m || board[x][y] != 'M')
            return 0;
        return 1;
    }
};